package com.ll.micrograss.grass.service.Impl;

import com.ll.api.feign.PointFeign;
import com.ll.common.core.entity.Grass;
import com.ll.common.core.utils.UserUtils;
import com.ll.micrograss.grass.mapper.GrassMapper;
import com.ll.micrograss.grass.pojo.DTO.GrassDTO;
import com.ll.micrograss.grass.service.IGrassService;
import io.seata.spring.annotation.GlobalTransactional;
import org.checkerframework.checker.units.qual.A;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.stream.Collectors;

@Service
public class GrassServiceImpl implements IGrassService {

    @Autowired
    private GrassMapper grassMapper;

    @Autowired
    private PointFeign pointFeign;


    @GlobalTransactional(rollbackFor = Exception.class)
    @Override
    public void add(GrassDTO grassDTO) {
        Integer uid = UserUtils.getUId();
        Grass grass = new Grass();
        BeanUtils.copyProperties(grassDTO,grass);
        //方案一
//        StringBuilder imgPath1 = new StringBuilder();
//        for (int i = 0; i < grassDTO.getImgList().size(); i++){
//            imgPath1.append(grassDTO.getImgList().get(i));
//            if (i != grassDTO.getImgList().size() - 1){
//                imgPath1.append(",");
//            }
//        }
//        System.out.println(imgPath1);
        //方案二(将图片路径的list用,拼接,用于分割各个路径,接收多张图片)
        String imagePath2 = String.join(",", grassDTO.getImgList());
        System.out.println(imagePath2);
        //方案三
//        String imagePath3 = grassDTO.getImgList().stream().collect(Collectors.joining(","));
//        System.out.println(imagePath3);

        // TODO 内容审核(0不通过,1通过)
        grass.setCheckStatus(1);
        //发布人的id,通过id给用户添加相应信息.
        grass.setUid(uid);
        //图片路径,将上传的图片组路径传入.
        grass.setImgPath(imagePath2);
        grassMapper.insert(grass);
        //添加积分信息
        pointFeign.add(uid,3);

        System.out.println(1/0);
    }
}
